package com.example.orchid.paper.task;

import android.content.Context;

import com.example.orchid.paper.Constants;
import com.example.orchid.paper.db.NewsDataSource;
import com.example.orchid.paper.entity.NewsDetailEntity;
import com.example.orchid.paper.util.GsonUtils;

/**
 * 下载新闻详情页内容，Task
 * Created by Orchid on 2015-10-13.
 */
public class GetNewsDetailTask extends BaseGetContentTask {

    public GetNewsDetailTask(Context context, ResponseListener listener) {
        super(context, listener);
    }

    @Override
    protected String doInBackground(String... params) {
        String content = null;

        try {
            content = getUrl(Constants.Url.URL_DETAIL + params[0]);

            NewsDetailEntity newsDetailEntity = (NewsDetailEntity) GsonUtils.getEntity(content, NewsDetailEntity.class);

            isRefreshSuccess = newsDetailEntity != null;
        } catch (Exception e) {
            e.printStackTrace();
            isRefreshSuccess = false;
            this.mException = e;
        }

        isContentSame = checkIsContentSame(params[0], content);

        if (isRefreshSuccess && !isContentSame) {
            ((NewsDataSource) getDataSource()).insertOrUpdateNewsList(Constants.NEWS_DETAIL,
                    "detail_" + params[0], content);

        }
        return content;
    }
}
